package com.util;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.po.InformationInfo;

public class ExportExcel {
	
	
	
	public void exportExcel(List<InformationInfo> list,HttpServletResponse response){
		
		XSSFWorkbook workbook = new XSSFWorkbook();
		XSSFSheet sheet = workbook.createSheet();
        XSSFRow row = sheet.createRow(0);
        for(int i=0;i<StringFinal.headers.length;i++){
        	XSSFCell cell = row.createCell(i);
        	XSSFRichTextString text = new XSSFRichTextString(StringFinal.headers[i]);  
            cell.setCellValue(text);
        }
        String[] str = new String[16];
        int index = 1;
         for(InformationInfo info:list){
        	row = sheet.createRow(index);
        	str[0] =index+"";
            str[1] =info.getInfo_name();
            str[2] =info.getInfo_author();
            str[3] =info.getInfo_abstract();
            str[4] =info.getInfo_keyword();
            str[5] =info.getInfo_source();
            str[6] =info.getInfo_bm();
            str[7] =info.getInfo_time();
            str[8] =info.getInfo_wh();
            str[9] =info.getInfo_source();
            str[10] =info.getFirst_class();
            str[11] =info.getInfo_download();
            str[12] =info.getInfo_browser();
            str[13] =info.getInfo_tag();
            str[14] =info.getInfo_level();
            str[15] =info.getUser_id();
            int column = 0;
            for(String infor:str){
            	XSSFCell cell = row.createCell(column);
            	setCell(cell,infor);
            	column++;
            }
            index++;
	}
         try {
			getExportFile(workbook,response);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
}
	
	public void getExportFile(XSSFWorkbook workbook,HttpServletResponse response) throws IOException{
		
		BufferedOutputStream fos =null;
		try {
			response.setContentType("application/x-msdownload");
			response.setHeader("Content-Disposition", "attachment;filename=" + new String( StringFinal.filename.getBytes("gb2312"), "ISO8859-1" ));
			fos = new BufferedOutputStream(response.getOutputStream());  
            workbook.write(fos);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			if(fos !=null)
				fos.close();
		}
	}
	public void setCell(XSSFCell cell,String value){
		String textValue = null;
		if( value!= null && value != ""){  
            textValue = value.toString();  
        }  
        if (textValue != null) {  
            XSSFRichTextString richString = new XSSFRichTextString(textValue);  
            cell.setCellValue(richString);  
        } 
	}
}
